﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ServicesSheet_OfficesConfirm : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["departmentId"] = "0";
            ViewState["year"] = "0";
            ViewState["month"] = "0";
            ViewState["EmpId"] = "";
            InitializeComponent();

            Master.DataSource = WMaster;


            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "~/Help/ServicesSheet/办公用品领用管理.htm";
            master.PageName = "办公品领用";
            master.PicUrl = "";
            master.IntiBar();

        }
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServicesSheet_OfficesConfirm));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        string where = "1=1";
        if (ViewState["departmentId"] != null)
        {
            if (ViewState["departmentId"].ToString() != "0")
                where += " AND Department.DepartmentId='" + ViewState["departmentId"].ToString() + "'";
        }
        if (ViewState["year"] != null)
        {
            if (ViewState["year"].ToString() != "0")
                where += " AND Services_OfficeSupplies.SuppliesYear='" + ViewState["year"].ToString() + "'";
        }
        if (ViewState["month"] != null)
        {
            if (ViewState["month"].ToString() != "0")
                where += " AND Services_OfficeSupplies.SuppliesMonth='" + ViewState["month"].ToString() + "'";
        }
        if (ViewState["EmpId"] != null)
        {
            if (ViewState["EmpId"].ToString() != "")
                where += " AND Employees.EmpName like '%" + ViewState["EmpId"].ToString() + "%'";
        }
        where += " AND Services_OfficeSupplies.FlowFlag='Z'";
        this.WMaster.SetWhere(where);
        this.WMaster.RemoteName = "ServiceSheet.View_Services_OfficeSupplies";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

    protected void wgvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblData = (Label)e.Row.FindControl("Label1");
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (drv != null && lblData != null)
            {
                lblData.Text = GetYearMonth(drv.Row["SuppliesYear"], drv.Row["SuppliesMonth"]);
            }

            CheckBox cb = (CheckBox)e.Row.FindControl("CheckBox1");
            if (cb != null)
            {
                if (Convert.IsDBNull(drv.Row["IsConfirm"]) == true)
                    cb.Checked = false;
                else
                    cb.Checked = drv.Row["IsConfirm"].ToString() == "False" ? false : true;
            }

        }

    }

    public String GetYearMonth(object InYear, object InMonth)
    {
        try
        {
            int IY = (int)InYear;
            int IM = (int)InMonth;
            return IY.ToString() + "/" + IM.ToString();
        }
        catch
        {
            return "";
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string departmentId = this.DepartmentDList1.SelectDepmanetId;
        string strYear = this.DSelYearMonth1.SelectYear;
        string strMonth = this.DSelYearMonth1.SelectMonth;
        string strEmpId = SelUser.EmpName;

        ViewState["departmentId"] = departmentId;
        ViewState["year"] = strYear;
        ViewState["month"] = strMonth;
        ViewState["EmpId"] = strEmpId;

        this.GridBind();
    }
    //protected void btnClear_Click(object sender, EventArgs e)
    //{
    //    this.QueryEmp.BindingValue = "";
    //    this.QueryEmp.BindingText = "";

    //    this.DepartmentDList1.SelectDepmanetId = "0";
    //    this.DSelYearMonth1.SelectYear = "0";
    //    this.DSelYearMonth1.SelectMonth = "0";

    //    ViewState["departmentId"] = "0";
    //    ViewState["year"] = "0";
    //    ViewState["month"] = "0";
    //    ViewState["EmpId"] = "";

    //    this.GridBind();
    //}
    //protected void wgvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    if (e.CommandName == "SelectAll")
    //    {
    //        for (int i = 0; i < this.wgvMaster.Rows.Count; i++)
    //        {
    //            CheckBox cbConfirm=(CheckBox)this.wgvMaster.Rows[i].FindControl("CheckBox1");
    //            if (cbConfirm != null)
    //                cbConfirm.Checked = true;
    //        }
    //    }
    //    else if (e.CommandName == "SelectOther")
    //    {
    //        for (int i = 0; i < this.wgvMaster.Rows.Count; i++)
    //        {
    //            CheckBox cbConfirm = (CheckBox)this.wgvMaster.Rows[i].FindControl("CheckBox1");
    //            if (cbConfirm != null)
    //            {
    //                if (cbConfirm.Checked == true)
    //                    cbConfirm.Checked = false;
    //                else
    //                    cbConfirm.Checked = true;
    //            }
    //        }
    //    }
    //    else if (e.CommandName == "Save")
    //    {


    //        ArrayList arr1 = new ArrayList();//选中的
    //        ArrayList arr2 = new ArrayList();//未选的

    //        for (int i = 0; i < this.wgvMaster.Rows.Count; i++)
    //        {
    //            string[] str1 = new string[2];   
    //            CheckBox cbConfirm = (CheckBox)this.wgvMaster.Rows[i].FindControl("CheckBox1");
    //            HiddenField hfSuId = (HiddenField)this.wgvMaster.Rows[i].FindControl("HiddenField1");
    //            HiddenField hfDeId = (HiddenField)this.wgvMaster.Rows[i].FindControl("HiddenField2");
    //            if (cbConfirm != null)
    //            {
    //                if (cbConfirm.Checked == true)
    //                {
    //                    str1[0] = hfSuId.Value;//this.wgvMaster.Rows[i].Cells[0].Text;
    //                    str1[1] = hfDeId.Value;//this.wgvMaster.Rows[i].Cells[1].Text;
    //                    arr1.Add(str1);
    //                }
    //                else
    //                {
    //                    str1[0] = hfSuId.Value;//this.wgvMaster.Rows[i].Cells[0].Text;
    //                    str1[1] = hfDeId.Value;//this.wgvMaster.Rows[i].Cells[1].Text;
    //                    arr2.Add(str1);
    //                }
    //            }
    //        }

    //        object[] objParam = new object[2];
    //        objParam[0] = arr1;
    //        objParam[1] = arr2;

    //        object[] myRet = Srvtools.CliUtils.CallMethod("ServiceSheet", "Exec_OfficeConfirm", objParam);
    //        bool boo = false;
    //        if (myRet != null && (int)myRet[0] == 0)
    //        {
    //            if (myRet[1] is bool)
    //                boo = (bool)myRet[1];
    //        }
    //        if (boo)
    //            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('保存成功！')", true);
    //        else
    //            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('保存失败！')", true);
    //    }
    //}

    private void GridBind()
    {
        InitializeComponent();

        Master.DataSource = WMaster;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        ArrayList arr1 = new ArrayList();//选中的
        ArrayList arr2 = new ArrayList();//未选的

        for (int i = 0; i < this.wgvMaster.Rows.Count; i++)
        {
            string[] str1 = new string[2];
            CheckBox cbConfirm = (CheckBox)this.wgvMaster.Rows[i].FindControl("CheckBox1");
            HiddenField hfSuId = (HiddenField)this.wgvMaster.Rows[i].FindControl("HiddenField1");
            HiddenField hfDeId = (HiddenField)this.wgvMaster.Rows[i].FindControl("HiddenField2");
            if (cbConfirm != null)
            {
                if (cbConfirm.Checked == true)
                {
                    str1[0] = hfSuId.Value;//this.wgvMaster.Rows[i].Cells[0].Text;
                    str1[1] = hfDeId.Value;//this.wgvMaster.Rows[i].Cells[1].Text;
                    arr1.Add(str1);
                }
                else
                {
                    str1[0] = hfSuId.Value;//this.wgvMaster.Rows[i].Cells[0].Text;
                    str1[1] = hfDeId.Value;//this.wgvMaster.Rows[i].Cells[1].Text;
                    arr2.Add(str1);
                }
            }
        }

        object[] objParam = new object[2];
        objParam[0] = arr1;
        objParam[1] = arr2;

        object[] myRet = Srvtools.CliUtils.CallMethod("ServiceSheet", "Exec_OfficeConfirm", objParam);

        bool boo = false;
        if (myRet != null && (int)myRet[0] == 0)
        {
            if (myRet[1] is bool)
                boo = (bool)myRet[1];
        }
        if (boo)
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('保存成功！')", true);
        else
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('保存失败！')", true);
    }
}
